﻿using System;

namespace Ping.Core
{
	public static class Extensions
	{
		public static string FormatSize(long size)
		{
			if (size < 1024)
				return string.Format("{0:F2} iB", size);
			if (size < 1024 * 1024)
				return string.Format("{0:F2} KiB", size / 1024.0);
			if (size < 1024 * 1024 * 1024)
				return string.Format("{0:F2} MiB", size / (1024.0 * 1024.0));

			return string.Format("{0:F2} GiB", size / (1024.0 * 1024.0 * 1024.0));
		}

		public static string Format(this TimeSpan interval)
		{
			if (interval < TimeSpan.FromSeconds(1))
				return string.Format("{0:F0} ms", interval.TotalMilliseconds);
			if (interval < TimeSpan.FromMinutes(1))
				return string.Format("{0:F0} s", interval.TotalSeconds);
			if (interval < TimeSpan.FromHours(1))
				return string.Format("{0:F0} m", interval.TotalMinutes);
			if (interval < TimeSpan.FromDays(1))
				return string.Format("{0:F0} h", interval.TotalHours);

			return string.Format("{0:F0} d", interval.TotalDays);
		}
	}
}